/**
 * Created by liubin on 2014/8/31.
 */
define(['angular', 'controllers', 'login/loginService', 'services'], function (angular, controllers) {
    'use strict';
    controllers.controller('loginCtrl', ['$scope', '$window', 'loginService','alertService','$location',
    function($scope, $window, loginService,alertService, $location){
        $scope.login = function (e) {
            console.log($scope.loginInfo);
            alertService.init($scope);
            loginService.login(
                // 实例信息
                {},
                // post 参数
                {
                    name: $scope.loginInfo.name,
                    pwd: $scope.loginInfo.pwd,
                    remember: $scope.loginInfo.remember
                },
                // 更新成功的方法
                function (success) {
                    console.log(success);
                    if (success.success) {
                        var lo = $location.search();
                        $window.location.href = lo.next?lo.next:'/';
                    }
                    else {
                        console.log(success.messages);
                        angular.forEach(success.messages, function (value, key) {
                            alertService.addAlert({
                                msg:  value,
                                type: 'danger'
                                //danger: true
                            });
                        });
                    }
                },
                function (error) {
                    console.log('error');
                }
            );
        }
    }]);
});